var KOC = require("koc.common.utils");

var Config = require("../config");
var CommonUtils = require("../lib/common-utils");

var FileBuilder = module.exports = {
    Common: {
        TempFilePath: function (filename) {
            return Config.Common.Physical_Path.File_Common_Temp + filename;
        },
        TempFileBuilder: function (code, filename, callback) {
            KOC.FSExtra.writeFile(FileBuilder.Common.TempFilePath(filename), code, function (err) {
                if (!callback || typeof callback !== "function") {
                    return;
                }
                if (err) {
                    callback(KOC.ReturnValue.New({
                        hasError: true,
                        message: err.message
                    }));
                    return;
                }
                callback(KOC.ReturnValue.New());
            });
        },
        TempFileRemove: function (filename, callback) {
            KOC.FSExtra.remove(FileBuilder.Common.TempFilePath(filename), function (err) {
                if (!callback || typeof callback !== "function") {
                    return;
                }
                if (err) {
                    callback(KOC.ReturnValue.New({
                        hasError: true,
                        message: err.message
                    }));
                    return;
                }
                callback(KOC.ReturnValue.New());
            });
        }
    },
    User: {
        Path: function (User_ID) {
            return Config.Common.Physical_Path.File_User + (User_ID ? User_ID + "/" : "");
        },
        Url: function (User_ID, Type) {
            return Config.Common.Virtual_Path.Utils_CDN + "service/img/user/" + Type + "/" + (User_ID ? User_ID + "/" : "");
        },
        Avatar: {
            Path: function (User_ID, Width, Height) {
                Width = KOC.StringUtils.ToInt(Width, -1);
                Height = KOC.StringUtils.ToInt(Height, -1);
                return FileBuilder.User.Path(User_ID) + "avatar" + ((Width > 0 || Height > 0) ? ("." + Width + "." + Height) : "") + ".jpg";
            },
            Url: function (User_ID, Width, Height) {
                Width = KOC.StringUtils.ToInt(Width, -1);
                Height = KOC.StringUtils.ToInt(Height, -1);
                return FileBuilder.User.Url(User_ID, "avatar") + ((Width > 0 || Height > 0) ? ("." + Width + "." + Height) : "") + ".jpg";
            },
            UrlAll: function (User_ID) {
                var _Size = [
                    [80, 80],
                    [54, 54]
                ];
                var _Urls = [];
                KOC.Lodash.forEach(_Size, function (ThisValue) {
                    _Urls.push(FileBuilder.User.Url(User_ID, "avatar") + ((ThisValue[0] > 0 || ThisValue[1] > 0) ? ( ThisValue[0] + "." + ThisValue[1]) : "") + ".jpg");
                });
                return _Urls;
            },
            Build: function (User_ID, Path, callback) {
                if (arguments.length < 2 || arguments.length > 3) {
                    return;
                }
                User_ID = KOC.StringUtils.ToInt(arguments[0], -1);
                if (arguments.length <= 2) {
                    Path = null;
                    callback = arguments[1];
                } else {
                    Path = arguments[1];
                    callback = arguments[2];
                }
                if (User_ID <= 0) {
                    callback(KOC.ReturnValue.New({
                        hasError: true,
                        message: "传入参数错误"
                    }));
                    return;
                }
                KOC.Async.series([
                    //创建文件夹
                    function (callback) {
                        KOC.FSExtra.mkdirs(FileBuilder.User.Path(User_ID), function (err) {
                            if (err) {
                                callback(new KOC.ReturnValue.New({
                                    hasError: true,
                                    message: err.message
                                }));
                                return;
                            }
                            callback();
                        });
                    },
                    //移动文件
                    function (callback) {
                        if (!Path || Path == "") {
                            callback();
                            return;
                        }
                        KOC.FSExtra.move(Path, FileBuilder.User.Avatar.Path(User_ID), {
                            clobber: true
                        }, function (err) {
                            if (err) {
                                callback(new KOC.ReturnValue.New({
                                    hasError: true,
                                    message: err.message
                                }));
                                return;
                            }
                            callback();
                        });
                    },
                    //生成文件
                    function (callback) {
                        Path = FileBuilder.User.Avatar.Path(User_ID);
                        KOC.Async.each([
                            ["Cut_Middle", 80, 80],
                            ["Cut_Middle", 54, 54]
                        ], function (ThisValue, callback) {
                            KOC.ImageUtils.Resize(ThisValue[0], Path, FileBuilder.User.Avatar.Path(User_ID, ThisValue[1], ThisValue[2]), ThisValue[1], ThisValue[2], function () {
                                callback();
                            });
                        }, function () {
                            callback();
                        });
                    }
                ], function (retValue) {
                    callback(retValue || KOC.ReturnValue.New());
                });
            }
        },
        Cover: {
            Path: function (User_ID, Width, Height) {
                Width = KOC.StringUtils.ToInt(Width, -1);
                Height = KOC.StringUtils.ToInt(Height, -1);
                return FileBuilder.User.Path(User_ID) + "cover" + ((Width > 0 || Height > 0) ? ("." + Width + "." + Height) : "") + ".jpg";
            },
            Url: function (User_ID, Width, Height) {
                Width = KOC.StringUtils.ToInt(Width, -1);
                Height = KOC.StringUtils.ToInt(Height, -1);
                return FileBuilder.User.Url(User_ID, "cover") + ((Width > 0 || Height > 0) ? ("." + Width + "." + Height) : "") + ".jpg";
            },
            Build: function (User_ID, Path, callback) {
                if (arguments.length < 2 || arguments.length > 3) {
                    return;
                }
                User_ID = KOC.StringUtils.ToInt(arguments[0], -1);
                if (arguments.length <= 2) {
                    Path = null;
                    callback = arguments[1];
                } else {
                    Path = arguments[1];
                    callback = arguments[2];
                }
                if (User_ID <= 0) {
                    callback(KOC.ReturnValue.New({
                        hasError: true,
                        message: "传入参数错误"
                    }));
                    return;
                }
                KOC.Async.series([
                    //创建文件夹
                    function (callback) {
                        KOC.FSExtra.mkdirs(FileBuilder.User.Path(User_ID), function (err) {
                            if (err) {
                                callback(new KOC.ReturnValue.New({
                                    hasError: true,
                                    message: err.message
                                }));
                                return;
                            }
                            callback();
                        });
                    },
                    //移动文件
                    function (callback) {
                        if (!Path || Path == "") {
                            callback();
                            return;
                        }
                        KOC.FSExtra.move(Path, FileBuilder.User.Cover.Path(User_ID), {
                            clobber: true
                        }, function (err) {
                            if (err) {
                                callback(new KOC.ReturnValue.New({
                                    hasError: true,
                                    message: err.message
                                }));
                                return;
                            }
                            callback();
                        });
                    },
                    //生成文件
                    function (callback) {
                        Path = FileBuilder.User.Cover.Path(User_ID);
                        KOC.Async.each([
                            ["Cut_Middle", 442, 442],
                            ["Cut_Middle", 294, 294],
                            ["Cut_Middle", 200, 200],
                            ["Cut_Middle", 146, 146]
                        ], function (ThisValue, callback) {
                            KOC.ImageUtils.Resize(ThisValue[0], Path, FileBuilder.User.Cover.Path(User_ID, ThisValue[1], ThisValue[2]), ThisValue[1], ThisValue[2], function () {
                                callback();
                            });
                        }, function () {
                            callback();
                        });
                    }
                ], function (retValue) {
                    callback(retValue || KOC.ReturnValue.New());
                });
            }
        },
        IDCard: {
            Path: function (User_ID, Front) {
                return FileBuilder.User.Path(User_ID) + "idcard-" + (KOC.Lodash.includes(["a", "b"], Front) ? Front : "a") + ".jpg";
            },
            Url: function (User_ID, Front) {
                return FileBuilder.User.Url(User_ID) + "idcard-" + (KOC.Lodash.includes(["a", "b"], Front) ? Front : "a") + ".jpg";
            },
            UrlAll: function (User_ID, Type) {
                return Config.Common.Virtual_Path.Utils_CDN + "service/img/user/" + Type + "/" + (User_ID ? User_ID : "") + ".jpg";
            },
            Build: function (User_ID, Front, Path, callback) {
                if (User_ID <= 0) {
                    callback(KOC.ReturnValue.New({
                        hasError: true,
                        message: "传入参数错误"
                    }));
                    return;
                }
                KOC.Async.series([
                    //创建文件夹
                    function (callback) {
                        KOC.FSExtra.mkdirs(FileBuilder.User.Path(User_ID), function (err) {
                            if (err) {
                                callback(new KOC.ReturnValue.New({
                                    hasError: true,
                                    message: err.message
                                }));
                                return;
                            }
                            callback();
                        });
                    },
                    //移动文件
                    function (callback) {
                        if (!Path || Path == "") {
                            callback();
                            return;
                        }
                        KOC.FSExtra.move(Path, FileBuilder.User.IDCard.Path(User_ID, Front), {
                            clobber: true
                        }, function (err) {
                            if (err) {
                                callback(new KOC.ReturnValue.New({
                                    hasError: true,
                                    message: err.message
                                }));
                                return;
                            }
                            callback();
                        });
                    }
                ], function (retValue) {
                    callback(retValue || KOC.ReturnValue.New());
                });
            }
        }
    },
    UserReport: {
        Path: function (User_ID) {
            return Config.Common.Physical_Path.File_Common_Report + (User_ID ? User_ID + "/" : "");
        },
        Report: {
            Path: function (User_ID, name) {
                return FileBuilder.UserReport.Path(User_ID) + "report-" + name + ".jpg";
            },
            Build: function (User_ID, Path, callback) {
                if (User_ID <= 0) {
                    callback(KOC.ReturnValue.New({
                        hasError: true,
                        message: "传入参数错误"
                    }));
                    return;
                }
                var report_name = KOC.Uuid.v4();
                var _Data_Path = null;
                KOC.Async.series([
                    //创建文件夹
                    function (callback) {
                        KOC.FSExtra.mkdirs(FileBuilder.UserReport.Path(User_ID), function (err) {
                            if (err) {
                                callback(new KOC.ReturnValue.New({
                                    hasError: true,
                                    message: err.message
                                }));
                                return;
                            }
                            callback();
                        });
                    },
                    //移动文件
                    function (callback) {
                        if (!Path || Path == "") {
                            callback();
                            return;
                        }
                        KOC.FSExtra.move(Path, FileBuilder.UserReport.Report.Path(User_ID, report_name), {
                            clobber: true
                        }, function (err) {
                            if (err) {
                                callback(new KOC.ReturnValue.New({
                                    hasError: true,
                                    message: err.message
                                }));
                                return;
                            }
                            _Data_Path = report_name;
                            callback();
                        });
                    }
                ], function (retValue) {
                    if (retValue) {
                        callback(retValue);
                        return;
                    }
                    callback(KOC.ReturnValue.New({
                        returnObject: _Data_Path
                    }));
                });
            }
        }
    },
    UserEvent: {
        Path: function (User_ID) {
            return Config.Common.Physical_Path.File_Common_Event + (User_ID ? User_ID + "/" : "");
        },
        Event: {
            Path: function (User_ID, name) {
                return FileBuilder.UserEvent.Path(User_ID) + "Event-" + name + ".jpg";
            },
            Build: function (User_ID, Path, callback) {
                if (User_ID <= 0) {
                    callback(KOC.ReturnValue.New({
                        hasError: true,
                        message: "传入参数错误"
                    }));
                    return;
                }
                var report_name = KOC.Uuid.v4();
                var _Data_Path = null;
                KOC.Async.series([
                    //创建文件夹
                    function (callback) {
                        KOC.FSExtra.mkdirs(FileBuilder.UserEvent.Path(User_ID), function (err) {
                            if (err) {
                                callback(new KOC.ReturnValue.New({
                                    hasError: true,
                                    message: err.message
                                }));
                                return;
                            }
                            callback();
                        });
                    },
                    //移动文件
                    function (callback) {
                        if (!Path || Path == "") {
                            callback();
                            return;
                        }
                        KOC.FSExtra.move(Path, FileBuilder.UserEvent.Event.Path(User_ID, report_name), {
                            clobber: true
                        }, function (err) {
                            if (err) {
                                callback(new KOC.ReturnValue.New({
                                    hasError: true,
                                    message: err.message
                                }));
                                return;
                            }
                            _Data_Path = report_name;
                            callback();
                        });
                    }
                ], function (retValue) {
                    if (retValue) {
                        callback(retValue);
                        return;
                    }
                    callback(KOC.ReturnValue.New({
                        returnObject: _Data_Path
                    }));
                });
            }
        }
    }
};